//
//  EmptyConfig.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/6/8.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import Gifu

// MARK: - gif 图片的 view
class EmptyGifView: UIView {
    
    var imageName: String?
    var imageWidth: CGFloat = 120.0
    var imageWidthHeightRatio: CGFloat = 2.0/3.0
    
    var imageView = GIFImageView.init()
    
    var gifData: Data?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(imageName name: String, width: CGFloat = 120.0, imageWidthHeightRatio ratio: CGFloat = 2.0/3.0) {
        self.init()
        self.imageName = name
        self.imageWidth = width
        self.imageWidthHeightRatio = ratio
        self.loadViews()
    }
    
    convenience init(gifData data: Data, width: CGFloat = 120.0, imageWidthHeightRatio ratio: CGFloat = 2.0/3.0) {
        self.init()
        self.gifData = data
        self.imageWidth = width
        self.imageWidthHeightRatio = ratio
        self.loadDataViews()
    }
    
    func loadDataViews() {
        addSubview(self.imageView)
        self.imageView.snp.makeConstraints { make in
            make.center.equalTo(self)
        }
        guard let data = self.gifData else {
            return
        }
        self.imageView.animate(withGIFData: data)
    }
    
    func loadViews() {
        addSubview(self.imageView)
        self.imageView.snp.makeConstraints { make in
            make.center.equalTo(self)
        }
        guard let name = imageName else {
            return
        }
        self.imageView.animate(withGIFNamed: name)
    }
    
}

// MARK: - 空数据 配置
struct EmptyConfig {
    
    static let normal = EmptyDataSetConfigure(verticalOffset: -120, tipFont: UIFont.systemFont(ofSize: 14), tipColor: UIColor.gray, tipImage: UIImage(named: "page_nodata", in: Bundle.frameworkBundle(), compatibleWith: nil)!, spaceHeight: 15)
    
    static let loading = { () -> EmptyDataSetConfigure in
        var config = EmptyConfig.normal
        let spinner = UIActivityIndicatorView.init(style: .gray)
        spinner.startAnimating()
        config.customEmptyView = spinner
        config.tipStr = "数据加载中..."
        return config
    }()
    
    static let loadingGif = { () -> EmptyDataSetConfigure in
        var config = EmptyConfig.normal
//        config.customEmptyView = EmptyGifView(imageName: "page_loading.gif")
        let data = try! Data(contentsOf: URL(fileURLWithPath: Bundle.frameworkBundle().bundlePath + "/page_loading.gif"))
        config.customEmptyView = EmptyGifView(gifData: data)
        config.tipStr = "数据加载中..."
        return config
    }()
    
    static let noData = { () -> EmptyDataSetConfigure in
        var config = EmptyConfig.normal
        config.tipStr = "暂无数据！"
        return config
    }()
    
    static let success = { () -> EmptyDataSetConfigure in
        var config = EmptyConfig.normal
        config.tipStr = "加载成功！"
        return config
    }()
    
    static let failure = { () -> EmptyDataSetConfigure in
        var config = EmptyConfig.normal
        config.tipStr = "呃，页面加载失败。"
        config.buttonImageBlock = { _ in
            return UIImage(named: "empty_refresh_btn")
        }
        return config
    }()
    
}
